<template>
  <div class="page-header-index-wide">
    <a-card
      :bordered="false"
      title="个人信息"
      :bodyStyle="{ padding: '24px 0', height: '100%', marginBottom: '24px' }"
      :style="{ height: '100%' }"
    >
      <a-row>
        <a-col offset="1" :order="1" :md="24" :lg="18" :xl="14" :style="{ minHeight: '100px' }">
          <div style="padding: 30px 0 0 30px">
            <a-descriptions size="default" :column="1">
              <a-descriptions-item label="姓名">
                
                <div class="canUpdate">
                  <span>{{ userInfo.name }}</span>
                  <a-button type="link" @click="handleOpen('name')">编辑</a-button>
                </div>
              </a-descriptions-item>
              <a-descriptions-item label="手机号码">
                {{ userInfo.phone }}
              </a-descriptions-item>
              <a-descriptions-item label="密码">
                <div class="canUpdate">
                  <span>**** </span>
                  <a-button type="link" @click="handleOpen('password')">修改</a-button>
                </div>
              
              </a-descriptions-item>
              <a-descriptions-item label="余额"> {{ userInfo.balance || 0 }} 元 </a-descriptions-item>
              <!-- <a-descriptions-item label="用户类型">
                <a-badge
                  :color="userInfo.type | statusFilter(userTypeList, 'status')"
                  :text="userInfo.type | statusFilter(userTypeList, 'name')"
                />
              </a-descriptions-item> -->

              <a-descriptions-item label="注册时间">
                {{ userInfo.addTime }}
              </a-descriptions-item>
              <a-descriptions-item label="注册IP">
                {{ userInfo.registerIp }}
              </a-descriptions-item>
            </a-descriptions>
          </div>
        </a-col>
      </a-row>
    </a-card>
    <awm-password
      ref="passwordModal"
      :visible="password.visible"
      :loading="password.confirmLoading"
      @cancel="
        () => {
          handleCancel('password')
        }
      "
      @ok="handleOk"
    />
    <awm-name
      ref="nameModal"
      :visible="name.visible"
      :loading="name.confirmLoading"
      @cancel="
        () => {
          handleCancel('name')
        }
      "
      @ok="handleNameOk"
    />
    <avatar-modal ref="modal" @ok="setavatar" />
  </div>
</template>

<script>
import { RouteView } from '@/layouts'
import { baseMixin } from '@/store/app-mixin'
import { mapGetters } from 'vuex'
import AvatarModal from './AvatarModal'
import { colors, eachMap, userTypeList } from '@/utils/other'
import AwmPassword from './modules/Password'
import AwmName from './modules/Name'

export default {
  components: {
    RouteView,
    AvatarModal,
    AwmPassword,
    AwmName
  },
  mixins: [baseMixin],
  data() {
    return {
      // horizontal  inline
      mode: 'inline',

      openKeys: [],
      selectedKeys: [],

      // cropper
      preview: {},
      option: {
        img: '/avatar2.jpg',
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 100,
        autoCropHeight: 100,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1],
      },
      password: {
        visible: false,
        confirmLoading: false,
      },
      name: {
        visible: false,
        confirmLoading: false,
      },
      
      userTypeList,
      pageTitle: '',
    }
  },
  computed: {
    ...mapGetters({ userInfo: 'userInfo', headImg: 'headImg' }),
  },
  mounted() {
    this.updateMenu()
  },
  methods: {
    handleOpen(str) {
      this[str].visible = true
    },
    handleCancel(str) {
      this[str].visible = false
      const form = this.$refs[`${str}Modal`].form
      form?form.resetFields():'' // 清理表单数据（可不做）
    },
    handleOk() {
      const createModal = this.$refs.passwordModal
      const form = createModal.form
      this.password.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          this.$store
            .dispatch('UserChangePSW', values)
            .then((res) => {
              this.saveCB('password')
              this.$message.success('密码修改成功!')
            })
            .catch((err) => {
              this.saveCB('password')
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleNameOk(){
      const createModal = this.$refs.nameModal
      const form = createModal.form
      this.name.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          this.$store
            .dispatch('UserUpdateSelf', values)
            .then((res) => {
              this.saveCB('name')
              this.$message.success('姓名修改成功!')
            })
            .catch((err) => {
              this.saveCB('name')
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    saveCB(str) {
      const form = this.$refs[`${str}Modal`].form
      this[str].visible = false
      this[str].confirmLoading = false
      // 重置表单数据
      form.resetFields()
      // 刷新表格
    },
    onOpenChange(openKeys) {
      this.openKeys = openKeys
    },
    updateMenu() {
      const routes = this.$route.matched.concat()
      this.selectedKeys = [routes.pop().path]
    },
    setavatar(url) {
      let _j = {
        headImg: url,
      }
      this.$store.dispatch('UserUpdateSelf', _j).then((res) => {})
    },
  },
  watch: {
    $route(val) {
      this.updateMenu()
    },
  },
}
</script>

<style lang="less" scoped>
.account-settings-info-main {
  width: 100%;
  display: flex;
  height: 100%;
  overflow: auto;

  &.mobile {
    display: block;

    .account-settings-info-left {
      border-right: unset;
      border-bottom: 1px solid #e8e8e8;
      width: 100%;
      height: 50px;
      overflow-x: auto;
      overflow-y: scroll;
    }
    .account-settings-info-right {
      padding: 20px 40px;
    }
  }

  .account-settings-info-left {
    border-right: 1px solid #e8e8e8;
    width: 224px;
  }

  .account-settings-info-right {
    flex: 1 1;
    padding: 8px 40px;

    .account-settings-info-title {
      color: rgba(0, 0, 0, 0.85);
      font-size: 20px;
      font-weight: 500;
      line-height: 28px;
      margin-bottom: 12px;
    }
    .account-settings-info-view {
      padding-top: 12px;
    }
  }
}
.ant-upload-preview {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 100px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    bottom: 0;
    right: 0px;
    font-size: 1rem;
    padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }
  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -1rem;
      margin-top: -1rem;
      color: #d6d6d6;
    }
  }

  img,
  .mask {
    width: 100%;
    max-width: 180px;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
  }
}
/deep/ .ant-descriptions {
  .ant-descriptions-view {
    .ant-descriptions-row {
      .ant-descriptions-item {
        line-height: 20px;
      }
    }
  }
}
/deep/ .ant-descriptions-item-label {
  width: 150px;
  font-size: 16px;
}
.canUpdate{
  min-width: 150px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
